﻿using System.Text;
using System.Text.RegularExpressions;//
namespace EasyDater.Base
{
    //字符串操作类
    public class StringUtility
    {
        public static string GetCode(string code)
        {
            string[] s = SplitCode(code);
            return s[0].Trim();
        }
        public static string GetName(string code)
        {
            string[] s = SplitCode(code);
            return s[1].Trim();
        }
        public static string[] SplitCode(string code)
        {
            string[] s = null;
            if (code.Contains("-"))
                s = code.Split('-');
            else
                s = new string[2] { "", code };

            return s;
        }

        //去掉前面的全角空格
        public static string RemoveLeftSpace(string str)
        {
            string s = str.Trim();
            while (s.StartsWith(((char)12288).ToString()))
            {
                s = s.Substring(1);
            }
            return s;
        }
        //所有全角空格替换成半角空格
        public static string FullWidthToHalfWidth(string str)
        {
            byte[] t = Encoding.Default.GetBytes(str);
            for (int i = 0; i < t.Length; i++)
            {
                if ((t[i].ToString() == "161") && (t[i + 1].ToString() == "161"))
                {
                    t[i] = 32;
                    if (i + 1 == t.Length - 1)
                    {
                        t[i + 1] = 0;
                    }
                    else
                    {
                        for (int j = i + 1; j + 1 < t.Length; j++)
                        {
                            t[j] = t[j + 1];
                            if (j + 1 == t.Length - 1)
                            {
                                t[j + 1] = 0;
                            }
                        }
                    }
                }
            }
            return Encoding.Default.GetString(t);
        }

        /// <summary>
        /// 按字节数取出字符串的长度
        /// </summary>
        /// <param name="strTmp">要计算的字符串</param>
        /// <returns>字符串的字节数</returns>
        public static int GetByteCount(string strTmp)
        {
            int intCharCount = 0;
            for (int i = 0; i < strTmp.Length; i++)
            {
                if (System.Text.UTF8Encoding.UTF8.GetByteCount(strTmp.Substring(i, 1)) == 3)
                {
                    intCharCount = intCharCount + 2;
                }
                else
                {
                    intCharCount = intCharCount + 1;
                }
            }
            return intCharCount;
        }
        /// <summary>
        /// 删除不可见字符
        /// </summary>
        /// <param name="sourceString"></param>
        /// <returns></returns>
        public static string DeleteUnVisibleChar(string sourceString)
        {
            System.Text.StringBuilder sBuilder = new System.Text.StringBuilder(131);
            for (int i = 0; i < sourceString.Length; i++)
            {
                int Unicode = sourceString[i];
                if (Unicode >= 16)
                {
                    sBuilder.Append(sourceString[i].ToString());
                }
            }
            return sBuilder.ToString();
        }
        /// <summary>
        /// 过长的字符串，显示一部分，后以...结束
        /// 使用： <%# PLeft(Eval("Profile"), 56)%>
        /// </summary>
        /// <param name="content"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string PLeft(string content, int length)
        {
            if (string.IsNullOrWhiteSpace(content))
            {
                return string.Empty;
            }
            if (content.Length > length)
            {
                content = content.Substring(0, length - 2) + "...";
            }
            return content;
        }

        /// <summary>
        ///  根据位数左补齐
        /// </summary>
        /// <param name="str">要补齐的字符</param>
        /// <param name="num">位数</param>
        /// <returns></returns>
        public static string LeftAppend(string str, int num)
        {
            string newStr = "";
            for (int i = 0; i < num; i++)
            {
                newStr += "0";
            }
            newStr += str;
            newStr = newStr.Substring(str.Length);
            return newStr;
        }
        //是否仅仅包含数字
        public static bool IncludeNumeric(string str)
        {
            foreach (char c in str)
            {
                if (Char.IsNumber(c))
                {
                    return true;
                }
            }
            return false;
        }
        //是否包含汉字
        public static bool IncludeHZ(string str)
        {
            Regex reg = new Regex(@"[\u4e00-\u9fa5]");//正则表达式

            if (reg.IsMatch(str))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //首字符变小写
        public static string FirstLowercase(string str)
        {
            if (String.IsNullOrWhiteSpace(str))
            {
                return str;
            }
            return str.Substring(0, 1).ToLower() + str.Substring(1);
           
        }
        //首字符变大写
        public static string FirstUppercase(string str)
        {
            if (String.IsNullOrWhiteSpace(str))
            {
                return str;
            }
            return str.Substring(0, 1).ToUpper() + str.Substring(1);
            
        }

        //实现换行  用在gridview里面
        public string HtmlReturn(string TString)
        {
            if (TString != null)
            {
                TString = TString.Replace("\r", "<br>");
                TString = TString.Replace(" ", " ");
                return TString;
            }
            else
            {
                return TString = "无内容";
            }
        }
        /// <summary>   
        /// 删除SQL注入特殊字符   
        /// 
        /// </summary>   
        public static string SQLInjection(string sql)
        {
            if (!string.IsNullOrEmpty(sql))
            {
                sql = sql.Replace("'", "''");
                //过滤 ' --   
                string pattern1 = @"(\%27)|(\')|(\-\-)";
                //防止执行 ' or   
                string pattern2 = @"((\%27)|(\'))\s*((\%6F)|o|(\%4F))((\%72)|r|(\%52))";
                //防止执行sql server 内部存储过程或扩展存储过程   
                string pattern3 = @"\s+exec(\s|\+)+(s|x)p\w+";
                sql = Regex.Replace(sql, pattern1, string.Empty, RegexOptions.IgnoreCase);
                sql = Regex.Replace(sql, pattern2, string.Empty, RegexOptions.IgnoreCase);
                sql = Regex.Replace(sql, pattern3, string.Empty, RegexOptions.IgnoreCase);
            }
            return sql;
        }

        /// 处理网页中的HTML代码，并消除危险字符
        public static string HTMLEncode(string str)
        {
            //替换尖括号
            string strContent = "";
            if (str != string.Empty)
            {
                str = str.Replace("&", "&amp;");
                str = str.Replace("'", "''");
                str = str.Replace("\"", "&quot;");
                str = str.Replace(" ", "&nbsp;");
                str = str.Replace("<", "&lt;");
                str = str.Replace(">", "&gt;");
                str = str.Replace("\n", "<br>");
                str = str.Replace(((char)13).ToString(), "<br />");
                //str = str.Replace(((char)10).ToString(), "<br />");
                str = str.Replace(((char)10).ToString(), " ");
                strContent = str;
            }
            return strContent;
        }
    }
}
